﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace FileCopy
{
    public partial class mainForm : Form
    {
        Thread threadMoveFile;//迁移文件线程
        Thread threadCopyFile;//复制文件线程
        Thread threadRenameFile;//重命名线程
        Thread threadExcelLoader;//Excel导入线程
        MoveFile mvAction;
        CopyFile cfAction;
        RenameFile rfAction;
        string appCurrentDirectory;
        public mainForm()
        {
            InitializeComponent();
            appCurrentDirectory = System.IO.Directory.GetCurrentDirectory();
            //默认路径
          //  tbDirectory.Text = 
            tbDirectory.Text = appCurrentDirectory;  //迁移文件根目录
            tbCopyPath.Text = appCurrentDirectory; //复制文件根目录
            tbRenameDir.Text = appCurrentDirectory;  //重命名文件根目录
        }
        /// <summary>
        /// 浏览并设置需要迁移的文件目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btBrowse_Click(object sender, EventArgs e)
        {
            //设置文件夹
            try
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    tbDirectory.Text = dialog.SelectedPath;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "错误");
            }
        }
        /// <summary>
        /// 根据文件名前缀迁移文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btMoveByName_Click(object sender, EventArgs e)
        {
            try
            {
                
                tsslStatus.Text = "迁移中";
                string path = tbDirectory.Text;
                string ext = tbMoveExt.Text;
                int length = int.Parse(tbMoveFileLength.Text);
                string date_format=tbMoveDateFormat.Text;
                dgvMoveResult.Rows.Clear();
                mvAction = new MoveFile(this,MoveFile.MOVE_BY_PREFIX,path, ext,length,date_format);
                threadMoveFile = new Thread(new ThreadStart(mvAction.Move));
                threadMoveFile.IsBackground = true;
                threadMoveFile.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "错误");
            }
        }
        /// <summary>
        /// 根据文件名的时间迁移文件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btMoveByDate_Click(object sender, EventArgs e)
        {
            try
            {

                tsslStatus.Text = "迁移中";
                string path = tbDirectory.Text;
                string ext = tbMoveExt.Text;
                int length = int.Parse(tbMoveFileLength.Text);
                string date_format = tbMoveDateFormat.Text;
                dgvMoveResult.Rows.Clear();
                if(rbCreateDate.Checked)
                    mvAction = new MoveFile(this, MoveFile.MOVE_BY_CREATE_DATE, path, ext, length, date_format);
                else
                    mvAction = new MoveFile(this, MoveFile.MOVE_BY_MODIFY_DATE, path, ext, length, date_format);

                threadMoveFile = new Thread(new ThreadStart(mvAction.Move));
                threadMoveFile.IsBackground = true;
                threadMoveFile.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "错误");
            }
        }

        /// <summary>
        /// 载入复制文件列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btLoadCopyList_Click(object sender, EventArgs e)
        {
            try
            {
                //打开文件
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件夹";
                dialog.Filter = "Excel文件|*.xls;*.xlsx";
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    dgvCopyList.Rows.Clear();
                    string file = dialog.FileName;

                    ExcelLoader excelLoader = new ExcelLoader(this, file, dgvCopyList);
                    threadExcelLoader = new Thread(new ThreadStart(excelLoader.Load));
                    threadExcelLoader.IsBackground = true;
                    threadExcelLoader.Start();
                }               
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
        /// <summary>
        /// 开始复制提取文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btCopyStart_Click(object sender, EventArgs e)
        {
            try
            {
                dgvCopyFail.Rows.Clear();
                tsslStatus.Text = "提取中";
                string path = tbCopyPath.Text;
                string ext = tbExt.Text;
                cfAction = new CopyFile(this, path, ext);
                threadCopyFile = new Thread(new ThreadStart(cfAction.Copy));
                threadCopyFile.IsBackground = true;
                threadCopyFile.Start();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "错误");
            }

        }
        /// <summary>
        /// 复制的图片所在文件夹指定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btCopyBrowse_Click(object sender, EventArgs e)
        {
            //设置文件夹
            try
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    tbCopyPath.Text = dialog.SelectedPath;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "错误");
            }
        }
        /// <summary>
        /// 导出缺失文件列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btExportCopyFail_Click(object sender, EventArgs e)
        {
            if (dgvCopyFail.Rows.Count <= 0)
            {
                MessageBox.Show("没有缺失数据");
                return;
            }
            string title = "缺失文件";
            string fileName = "";//保存的excel文件名
            bool isShowExcel = false;//是否显示Excel
            /*保存对话框*/
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "导出Excel(*.xls)|*.xls";
            sfd.FileName = title + DateTime.Now.ToString("yyyyMMddhhmmss");

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                fileName = sfd.FileName;
                /*建立Excel对象*/
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                if (excel == null)
                {
                    MessageBox.Show("无法创建Excel对象,可能您的计算机未安装Excel!");
                    return;
                }
                try
                {
                    excel.Application.Workbooks.Add(true);
                    excel.Visible = isShowExcel;
                    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
                    worksheet.Cells[1, 1] = "文件名";
                    worksheet.Cells[1, 2] = "属性1";
                    worksheet.Cells[1, 3] = "属性2";
                    worksheet.Cells[1, 4] = "属性3";
                    int row_count = dgvCopyFail.Rows.Count;
                    for (int i = 0; i < row_count; i++)
                    {
                        worksheet.Cells[i + 2, 1] = "'" + dgvCopyFail.Rows[i].Cells["NoName"].Value.ToString();
                        worksheet.Cells[i + 2, 2] = "'" + dgvCopyFail.Rows[i].Cells["NoProp1"].Value.ToString();
                        worksheet.Cells[i + 2, 3] = "'" + dgvCopyFail.Rows[i].Cells["NoProp2"].Value.ToString();
                        worksheet.Cells[i + 2, 4] = "'" + dgvCopyFail.Rows[i].Cells["NoProp3"].Value.ToString();
                    }

                    worksheet.SaveAs(fileName, 56);//56为兼容模式，不加参数默认为当前xlsx格式。
                    MessageBox.Show("成功导出到"+fileName);
                }
                catch { }
                finally
                {
                    excel.Quit();
                    excel = null;
                    GC.Collect();
                }
            }
            return;
        }
        /// <summary>
        /// 导入重命名对应关系
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btLoadRename_Click(object sender, EventArgs e)
        {
            try
            {
                //打开文件
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件夹";
                dialog.Filter = "Excel文件|*.xls;*.xlsx";
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    dgvRenameMap.Rows.Clear();
                    string file = dialog.FileName;

                    ExcelLoader excelLoader = new ExcelLoader(this, file, dgvRenameMap);
                    threadExcelLoader = new Thread(new ThreadStart(excelLoader.Load));
                    threadExcelLoader.IsBackground = true;
                    threadExcelLoader.Start();
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "错误");
            }
        }
        /// <summary>
        /// 指定重命名的文件根目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btRenameBrowse_Click(object sender, EventArgs e)
        {
            //设置文件夹
            try
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = "请选择文件路径";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    tbRenameDir.Text= dialog.SelectedPath;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "错误");
            }
        }

        private void btRenamePreview_Click(object sender, EventArgs e)
        {
            string path = tbRenameDir.Text;
            string ext = tbRenameExt.Text;
            rfAction = new RenameFile(this, path, ext);
            dgvRenameResult.Rows.Clear();
            rfAction.renamePreview();
        }

        private void btRenameExec_Click(object sender, EventArgs e)
        {
            try
            {
                dgvRenameResult.Rows.Clear();
                tsslStatus.Text = "重命名中";
                string path = tbRenameDir.Text;
                string ext = tbRenameExt.Text;
                rfAction = new RenameFile(this, path, ext);
                threadRenameFile = new Thread(new ThreadStart(rfAction.renameExec));
                threadRenameFile.IsBackground = true;
                threadRenameFile.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "错误");
            }
        }

       
    }
}
